/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.order.poa.controller;

import com.activiti.dao.model.Activiti_SubTaskInfoRO;
import com.activiti.dao.service.ActivitiService;
import com.activiti.dao.service.TaskSubService;
import com.erp.cux.workflow.controller.CuxControllerSupport;
import com.erp.dataset.service.DatasetCommonService;
import com.erp.hr.dao.model.HrDepartment;
import com.erp.hr.dao.model.HrStaffInfoRO;
import com.erp.hr.service.HrCommonService;
import com.erp.masterdata.common.service.MasterDataCommonService;
import com.erp.masterdata.vendor.service.MdVendorService;
import com.erp.order.poa.dao.model.PoAgreementHead;
import com.erp.order.poa.dao.model.PoAgreementHeadBase;
import com.erp.order.poa.dao.model.PoAgreementHeadCO;
import com.erp.order.poa.service.PoAgreementHeadService;
import com.erp.order.poaeco.service.PoAgreementHeadMService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.hibernate.exception.ConstraintViolationException;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.ShiroUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/web/poAgreementHeadArchived")
public class PoAgreementHeadArchivedWebController extends CuxControllerSupport {

    //日志处理
    private Logger logger = LoggerFactory.getLogger(PoAgreementHeadArchivedWebController.class);

    //服务层注入
    @Autowired
    private PoAgreementHeadService poAgreementHeadService;
    @Autowired
    private PoAgreementHeadMService poAgreementHeadMService;
    @Autowired
    private DatasetCommonService datasetCommonService;
    @Autowired
    private MasterDataCommonService masterDataCommonService;
    @Autowired
    private MdVendorService mdVendorService;
    @Autowired
    private HrCommonService hrCommonService;
    @Autowired
    private ActivitiService activitiService;
    @Autowired
    private TaskSubService taskSubService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private RepositoryService repositoryService;

    @Override
    public String getExceptionRedirectUrl() {
        return this.getFormatRedirectUrl("getPoHeadList");
    }
    /**
     * 跳转到合同归档列表
     */
    @RequestMapping("poaArchivedList")
    public String getPoAgreementHeadArchivedList(Pages pages, PoAgreementHeadCO poAgreementHeadCO, Model model) {
        //分页查询数据
        if (pages.getPage() == 0) {
            pages.setPage(1);
        }
        //分页查询数据 " and p.status in('CONFIRM','CONFIRM_FILENP')"
        StringBuffer dataAuthSql = new StringBuffer();
        dataAuthSql.append(" and p.status in('CONFIRM','CONFIRM_FILENP') ");
        List<PoAgreementHead> poAgreementHeadList = this.poAgreementHeadService.getDataObjectsForDataAuth(dataAuthSql.toString(), pages, poAgreementHeadCO);
        //合同性质
        Map poQualeMap = this.datasetCommonService.getPOType();
        //合同类型
        Map poTypeMap = this.datasetCommonService.getPOAType();
        //状态
        Map poStatusMap = this.datasetCommonService.getPOAStatus();
        //审批状态
        Map approveStatusMap = this.datasetCommonService.getApproveStatus();
        //获取供应商
        Map vendorMap = this.masterDataCommonService.getVendorMap();
        //查询状态
        Map searchStatusMap = new HashMap();
        searchStatusMap.put("CONFIRM","生效未归档");
        searchStatusMap.put("CONFIRM_FILENP","归档未付款");

        //页面属性设置
        model.addAttribute("poHeadList", poAgreementHeadList);
        model.addAttribute("pages", pages);
        model.addAttribute("poQualeMap", poQualeMap);
        model.addAttribute("poTypeMap", poTypeMap);
        model.addAttribute("poStatusMap", poStatusMap);
        model.addAttribute("approveStatusMap", approveStatusMap);
        model.addAttribute("vendorMap", vendorMap);
        model.addAttribute("searchStatusMap", searchStatusMap);
        return "basic.jsp?content=poa/poaArchivedList";
    }

    /**
     * 跳转合同归档页面
     */
    @RequestMapping("getPoHeadArchived")
    public String getPoAgreementHeadArchived(PoAgreementHeadBase poAgreementHead, Model model) {
        //查询要编辑的数据
        PoAgreementHeadBase poAgreementHeadTemp = this.poAgreementHeadService.getDataObject(poAgreementHead.getPoHeadCode());
        if (poAgreementHeadTemp != null) {
            poAgreementHead = poAgreementHeadTemp;
        }
        //合同性质
        Map poQualeMap = this.datasetCommonService.getPOType();
        //合同类型
        Map poTypeMap = this.datasetCommonService.getPOAType();
        //合同类别
        Map poCateMap = this.datasetCommonService.getPOACategory();
        //合同结算方式
        Map poSettleMap = this.datasetCommonService.getPOASettleStyle();
        //合同来源方式
        Map poSourceMap = this.datasetCommonService.getPOASourceMode();
        //币种
        Map currencyTypeMap = this.datasetCommonService.getCurrencyType();
        //获取供应商
        Map vendorMap = this.masterDataCommonService.getVendorMap();
        //状态
        Map poStatusMap = this.datasetCommonService.getPOAStatus();

        //获取当前用户职员信息
        HrStaffInfoRO staffInfo = this.hrCommonService.getStaffInfo(ShiroUtil.getPrincipal());
        HrDepartment hrDepartmentget=this.hrCommonService.getHrDepartmentParentCompany(staffInfo.getDepartmentCode());
        staffInfo.setCompanyName(hrDepartmentget.getDepartmentName());
        staffInfo.setCompanyCode(hrDepartmentget.getDepartmentCode());
        poAgreementHead.setArchivedDepart(staffInfo.getDepartmentName());
        poAgreementHead.setArchivedBy(staffInfo.getStaffName());

        //页面属性设置
        model.addAttribute("poHead", poAgreementHead);
        model.addAttribute("poQualeMap", poQualeMap);
        model.addAttribute("poTypeMap", poTypeMap);
        model.addAttribute("poCateMap", poCateMap);
        model.addAttribute("poSettleMap", poSettleMap);
        model.addAttribute("poSourceMap", poSourceMap);
        model.addAttribute("currencyTypeMap", currencyTypeMap);
        model.addAttribute("poStatusMap", poStatusMap);
        model.addAttribute("vendorMap", vendorMap);
        return "basic.jsp?content=poa/poaArchivedEdit";
    }

    /**
     * 合同归档
     */
    @RequestMapping("editPoHeadArchived")
    public String editPoAgreementHeadForArchived(@Valid PoAgreementHead poAgreementHead, BindingResult bindingResult, Model model, RedirectAttributes attr) {
        PoAgreementHead poAgreementHeadTemp = this.poAgreementHeadService.getDataObject(poAgreementHead.getPoHeadCode());
        try {
            poAgreementHeadTemp.setArchivedBy(poAgreementHead.getArchivedBy());
            poAgreementHeadTemp.setArchivedDepart(poAgreementHead.getArchivedDepart());
            poAgreementHeadTemp.setArchivedFileurl(poAgreementHead.getArchivedFileurl());
            poAgreementHeadTemp.setArchivedDate(new Date());
            poAgreementHeadTemp.setStatus("CONFIRM_FILENP");
            this.poAgreementHeadService.insertOrUpdateDataObject(poAgreementHeadTemp);
            attr.addFlashAttribute("hint", "success");
        } catch (Exception e) {
            if (e.getCause().getClass() == ConstraintViolationException.class) {
                //提示信息
                attr.addFlashAttribute("hint", "hint");
                return "redirect:poaArchivedList";
            } else {
                throw e;
            }
        }
        return "redirect:poaArchivedList";
    }

    /**
     * 跳转合同归档查看页面
     */
    @RequestMapping("getPoHeadArchivedView")
    public String getPoAgreementHeadArchivedView(PoAgreementHeadBase poAgreementHead, Model model) {
        //查询要编辑的数据
        PoAgreementHeadBase poAgreementHeadTemp = this.poAgreementHeadService.getDataObject(poAgreementHead.getPoHeadCode());
        if (poAgreementHeadTemp != null) {
            poAgreementHead = poAgreementHeadTemp;
            if(poAgreementHead.getProcessInstanceId()!=null){
                //根据流程id获取流程审批信息
                List<Activiti_SubTaskInfoRO> activiti_subTaskInfoList = taskSubService.getDataObjectsListOnlyLastTrue(poAgreementHead.getProcessInstanceId());
                model.addAttribute("subTaskInfoList", activiti_subTaskInfoList);
            }
        }
        //合同性质
        Map poQualeMap = this.datasetCommonService.getPOType();
        //合同类型
        Map poTypeMap = this.datasetCommonService.getPOAType();
        //合同类别
        Map poCateMap = this.datasetCommonService.getPOACategory();
        //合同结算方式
        Map poSettleMap = this.datasetCommonService.getPOASettleStyle();
        //合同来源方式
        Map poSourceMap = this.datasetCommonService.getPOASourceMode();
        //币种
        Map currencyTypeMap = this.datasetCommonService.getCurrencyType();
        //获取供应商
        Map vendorMap = this.masterDataCommonService.getVendorMap();
        //状态
        Map poStatusMap = this.datasetCommonService.getPOAStatus();

        //页面属性设置
        model.addAttribute("poHead", poAgreementHead);
        model.addAttribute("poQualeMap", poQualeMap);
        model.addAttribute("poTypeMap", poTypeMap);
        model.addAttribute("poCateMap", poCateMap);
        model.addAttribute("poSettleMap", poSettleMap);
        model.addAttribute("poSourceMap", poSourceMap);
        model.addAttribute("currencyTypeMap", currencyTypeMap);
        model.addAttribute("poStatusMap", poStatusMap);
        model.addAttribute("vendorMap", vendorMap);
        return "basic.jsp?content=poa/poaArchivedView";
    }
}
